package com.store.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.Result;
import com.store.app.ExitApplication;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.bean.GoodsBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;
import com.store.app.zxing.Capture_ZxingActivity;

import java.lang.reflect.Type;

/**
 * Created by Administrator on 2016/3/8.
 * 扫描商品
 */
public class GoodsCaptureActivity extends Capture_ZxingActivity implements IOnHttpListener {
    private static final int REQUEST_ADD = 1;
    private static final int REQUEST_SCAN = 2;
    public String barCode;
    private TextView tv_cap;
    private NetRequest netRequest;

    @Override
    public void onAfinalFail() {
        ToastUtils.showTextToast(this,"请检查本地网络!");
        dismissProgressDialog();
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            Type type = new TypeToken<GoodsBean>() {
            }.getType();
            Gson gson = new Gson();
            GoodsBean goods = gson.fromJson(result, type);
            if (goods != null && !TextUtils.isEmpty(goods.getItem_name())) {
                Intent intent = new Intent(this, AddGoodNewActivity.class);
                intent.putExtra("goods_bean", goods);
                startActivityForResult(intent, REQUEST_SCAN);
                finish();
            } else {
                Intent in = new Intent(this, AddGoodNewActivity.class);
                in.putExtra("barcode", barCode);
                startActivityForResult(in, REQUEST_ADD);
                finish();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();

        if (requestId == 1) {
            Intent in2 = new Intent(this, AddGoodNewActivity.class);
            in2.putExtra("barcode", barCode);
            startActivityForResult(in2, REQUEST_ADD);
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_capture2);
        Log.v("zyl","GoodsCaptureActivity");
        netRequest = new NetRequest(this);
        ExitApplication.getInstance().addActivity(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setInintView();
        findViewById();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            setResult(Activity.RESULT_OK, getIntent());
            finish();
        }
    }

    private void findViewById() {
        findViewById(R.id.retreat).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_cap = (TextView) findViewById(R.id.cap_tv);
        int width = LocationApplication.app.getDM().widthPixels;
        int high = LocationApplication.app.getDM().heightPixels;
        width = width / 8 * 3;
        high = high / 2 + width;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = high + MainActivity.dip2px(20);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tv_cap.setLayoutParams(layoutParams);
        tv_cap.setText("扫描商品上的条形码，即可自动添加商品");
    }

    /**
     * 处理扫描结果
     *
     * @param result
     * @param barcode
     */
    public void handleDecode(Result result, Bitmap barcode) {
        inactivityTimer.onActivity();
        playBeepSoundAndVibrate();
        String resultString = result.getText();
        barCode = resultString;
        if (resultString.equals("")) {
            Toast.makeText(GoodsCaptureActivity.this, "扫描错误!", Toast.LENGTH_SHORT).show();
        } else {
            showProgressDialog("查询商品", "加载中……");
            netRequest.requestItemFind(1, barCode);
        }
    }
}
